import {
	GLOBAL_DATA
} from '/@/api/constant'
import {
	getEnvs
} from "/@/utils/envs.js"
export function copyDomText(val) {
	const text = val;
	// 添加一个input元素放置需要的文本内容
	const input = document.createElement("input");
	input.value = text;
	document.body.appendChild(input);
	// 选中并复制文本到剪切板
	input.select();
	document.execCommand("copy");
	// 移除input元素
	document.body.removeChild(input);
}

export function downField(fileName) {
	const {
		envStr
	} = getEnvs()
	let path = GLOBAL_DATA[envStr]['fileUrl'];
	const down = document.createElement('a');
	down.style.display = 'none'; //隐藏,没必要展示出来
	let url = path + fileName;;
	console.log('url', url)
	fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
		down.href = URL.createObjectURL(blob)
		down.download = url
		document.body.appendChild(down)
		down.click()
		document.body.removeChild(down); //下载完成移除
	})


	// document.body.appendChild(down);
	// down.click();

}


export function downFieldFull(fileName) {

	const down = document.createElement('a');
	down.style.display = 'none'; //隐藏,没必要展示出来
	let url = fileName;;
	console.log('url', url)
	fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
		down.href = URL.createObjectURL(blob)
		down.download = url
		document.body.appendChild(down)
		down.click()
		document.body.removeChild(down); //下载完成移除
	})


	// document.body.appendChild(down);
	// down.click();

}


export function setThemeColor(color) {
	const el = document.documentElement;
	const body = document.querySelector("body");
	// const nprogress = document.querySelector("#nprogress .bar");
	// console.log(nprogress)
	el.style.setProperty("--el-color-primary", color);
	body.style.setProperty("--van-primary-color", color);
	// nprogress.style.setProperty("background", color);
	// 此行判断是否为白天/暗夜模式，可根据自身业务调整代码
	let mixColor = color;
	// 此行判断是否为白天/暗夜模式，可根据自身业务调整代码
	for (let i = 1; i < 10; i++) {
		el.style.setProperty(`--el-color-primary-light-${i}`, colourBlend(color, mixColor, i / 10));
		el.style.setProperty(`--el-color-primary-dark-${i}`, colourBlend(color, mixColor, i / 10));
	}
	el.style.setProperty(`--el-color-primary-dark-2`, colourBlend(color, mixColor, 0.2));

}

export function colourBlend(c1, c2, ratio) {
	ratio = Math.max(Math.min(Number(ratio), 1), 0)
	let r1 = parseInt(c1.substring(1, 3), 16)
	let g1 = parseInt(c1.substring(3, 5), 16)
	let b1 = parseInt(c1.substring(5, 7), 16)
	let r2 = parseInt(c2.substring(1, 3), 16)
	let g2 = parseInt(c2.substring(3, 5), 16)
	let b2 = parseInt(c2.substring(5, 7), 16)
	let r = Math.round(r1 * (1 - ratio) + r2 * ratio)
	let g = Math.round(g1 * (1 - ratio) + g2 * ratio)
	let b = Math.round(b1 * (1 - ratio) + b2 * ratio)
	r = ('0' + (r || 0).toString(16)).slice(-2)
	g = ('0' + (g || 0).toString(16)).slice(-2)
	b = ('0' + (b || 0).toString(16)).slice(-2)
	return '#' + r + g + b
}


// 定义时间格式化函数
export function formatTime(date) {
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function parseTime(time, pattern) {
	if ( !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		} else if (typeof time === 'string') {
			time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay(),
	}
	return format.replace(/{([ymdhisa])+}/g, (result, key) => {
		// @ts-ignore
		let value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') {
			return ['日', '一', '二', '三', '四', '五', '六'][value]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
}

export function getDaysInMonth(year, month) {
	const lastDay = new Date(year, month + 1, 0).getDate();
	return lastDay;
}
